<!--
 * @Author: fj
 * @LastEditors: fj
 * @description:
 * @updateInfo: 本次更新内容：
 * @Date: 2023-08-14 15:15:08
 * @LastEditTime: 2023-08-29 15:39:10
-->
<script setup lang="ts" name="WechatApplet">
import useFields from './fields'
import { msgApi } from '@/MainApp/apis/msg'
const { t } = useConfig()
const { fieldList, formKey } = useFields()
const { msgError, msgSuccess, msgBoxWarnAsync } = useMessage()
const listData = ref([])
const loading = ref(false)
const total = ref(0)
const pageData = ref()
const pageFullRef = ref()

const queryList = async () => {
  loading.value = true
  const params = {
    ...pageData.value
  }
  const { msg, success, data, total: pageTotal } = await msgApi.getWechatMsgList(params)
  listData.value = (success && data) ? data : []
  total.value = pageTotal
  !success && msgError(msg)
  loading.value = false
}
/**
 * @description: 表单提交数据
 * @param {*} form
 * @return {*}
 */
const handleAdd = async (form: any) => {
  const { msg, success } = form.id ? await msgApi.updateWechatMsg(form) : await msgApi.addWechatMsg(form)
  if (success)
    queryList()
  else
    msgError(msg)
}

/**
 * @description: 修改
 * @param {*} row
 * @return {*}
 */
const handleUpdate = (row: any) => {
  pageFullRef.value?.handCreateFormDialog(row)
}

/**
 * @description:
 * @param {*} row
 * @return {*}
 */
const handDeleteTemp = (row: any) => {
  msgBoxWarnAsync('删除确认', `确认删除[${row.name}] 吗?`, async () => {
    const { msg, success } = await msgApi.deleteWechatMsg(row.id)
    if (success) {
      msgSuccess(msg)
      queryList()
    }
    else { msgError(msg) }
  })
}
</script>

<template>
  <IkPageMain fixed>
    <IkPageFull
      ref="pageFullRef"
      v-model="pageData"
      header-title="微信小程序列表"
      :immediate="false"
      :fields="fieldList"
      :table-data="listData"
      :total="total"
      :loading="loading"
      :search="queryList"
      :is-edit="true"
      dialog-size="sl"
      role="$ADD-WECHAT"
      :form-key="formKey"
      @addFunction="handleAdd"
    >
      <template #operate="{ data }">
        <IkBtnContent>
          <IkSvgIcon
            name="icon-bianji"
            :title="t('action.modify')"
            show-bg
            pop
            :font-size="16"
            @click="handleUpdate(data.row)"
          />
          <IkSvgIcon
            name="icon-shanchu"
            show-bg
            pop
            :font-size="16"
            :title="t('action.delete')"
            @click="handDeleteTemp(data.row)"
          />
        </IkBtnContent>
      </template>
    </IkPageFull>
  </IkPageMain>
</template>
